package com.bootx.controller.api;

import com.bootx.controller.BaseController;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.model.ollama.OllamaStreamingChatModel;
import dev.langchain4j.model.zhipu.ZhipuAiStreamingChatModel;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author black
 */
@RestController
@RequestMapping("/api/write")
public class WriteController extends BaseController {

    @GetMapping(value = "/write")
    public Flux<String> write(String content, HttpServletResponse response) {
        response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE+";charset=utf-8");
        StreamingChatLanguageModel chatLanguageModel = OllamaStreamingChatModel.builder().baseUrl("http://127.0.0.1:11434").modelName("qwen2.5:3b").build();
        SystemMessage systemMessage = SystemMessage.from("自动扩写，拓展创作边界。根据用户的要求进行扩写。");
        ChatRequest chatRequest = ChatRequest.builder().messages(new ChatMessage[]{systemMessage,UserMessage.from(content)}).build();
        return Flux.create(emitter -> {
            chatLanguageModel.chat(chatRequest, new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String s) {
                    emitter.next(s);
                }

                @Override
                public void onCompleteResponse(ChatResponse chatResponse) {
                    emitter.complete();
                }

                @Override
                public void onError(Throwable throwable) {
                    emitter.error(throwable);
                }
            });
        });
    }

    @GetMapping(value = "/write1")
    public Flux<String> write1(String content, HttpServletResponse response) {
        response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE+";charset=utf-8");
        OllamaApi ollamaApi = new OllamaApi();
        ChatModel chatModel = OllamaChatModel.builder()
                .defaultOptions(OllamaOptions.builder()
                        .model("deepseek-r1:8b").build()
                ).ollamaApi(ollamaApi)
                .build();
        ChatClient client = ChatClient.builder(chatModel).defaultSystem("自动扩写，拓展创作边界。根据用户输入的内容和要求进行扩写。").build();
        return client.prompt(content)
                .stream()
                .chatResponse()
                .map(item->item.getResults().getFirst().getOutput().getText());

    }

    @GetMapping(value = "/deepSeek")
    public Flux<String> deepSeek(String content, HttpServletResponse response) {
        response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE+";charset=utf-8");
        ChatModel chatModel = OpenAiChatModel.builder()
                .defaultOptions(OpenAiChatOptions.builder()
                        .model("deepseek-chat").build()
                ).openAiApi(OpenAiApi.builder()
                        .baseUrl("https://api.deepseek.com")
                        .apiKey("sk-f9bc6f0edc42411b89f25e5b81d82835")
                        .build()
                ).build();
        ChatClient client = ChatClient.builder(chatModel).defaultSystem("自动扩写，拓展创作边界。根据用户输入的内容和要求进行扩写。不能少于1000字。").build();
        return client.prompt(content)
                .stream()
                .chatResponse()
                .map(item->item.getResults().getFirst().getOutput().getText());

    }

    @GetMapping(value = "/douBao")
    public Flux<String> douBao(String content, HttpServletResponse response) {
        response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE+";charset=utf-8");
        ChatModel chatModel = OpenAiChatModel.builder()
                .defaultOptions(OpenAiChatOptions.builder()
                        .model("doubao-1-5-pro-32k-250115").build()
                ).openAiApi(OpenAiApi.builder()
                        .baseUrl("https://ark.cn-beijing.volces.com/api")
                        .apiKey("082d1e98-122a-431a-a518-5b06222af657")
                        .build()
                ).build();
        ChatClient client = ChatClient.builder(chatModel).defaultSystem("自动扩写，拓展创作边界。根据用户输入的内容和要求进行扩写。不能少于1000字。").build();
        return client.prompt(content)
                .stream()
                .chatResponse()
                .map(item->item.getResults().getFirst().getOutput().getText());

    }

    @GetMapping(value = "/zhiPu")
    public Flux<String> zhiPu(String content, HttpServletResponse response) {
        response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE+";charset=utf-8");
        ZhiPuAiApi zhiPuAiApi = new ZhiPuAiApi("fcf2dd52fa3c49eaa3a01de73aa2fb2e.N6zH58SFN9GaeUiI");
        ChatModel chatModel = new ZhiPuAiChatModel(zhiPuAiApi, ZhiPuAiChatOptions.builder()
                .model("glm-4-long")
                .build());
        ChatClient client = ChatClient.builder(chatModel).defaultSystem("自动扩写，拓展创作边界。根据用户输入的内容和要求进行扩写。不能少于1000字。").build();
        return client.prompt(content)
                .stream()
                .chatResponse()
                .map(item->item.getResults().getFirst().getOutput().getText());

    }
}
